<?php

declare (strict_types = 1);

namespace app\common\service\user;

use app\common\service\BaseService;
use app\common\exception\AdminException;
use app\common\repository\user\UserTagCateRepository;

class UserTagCateService extends BaseService
{
    public function __construct(UserTagCateRepository $repository)
    {
        $this->repository = $repository;
    }

    /**
     * 获取列表
     *
     * @param $where
     * @return \think\Collection|\think\model\Collection
     */
    public function getList($where = []): \think\model\Collection|\think\Collection
    {
        return $this->repository->getByWhere($where, ['sort' => 'desc']);
    }

    /**
     * 获取详情信息
     *
     * @param $id
     * @return \app\common\model\BaseModel|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getInfo($id)
    {
        return $this->repository->getOne($id);
    }

    /**
     * 创建信息
     *
     * @param array $data
     * @return \app\common\model\BaseModel|array|\think\Collection
     * @throws \Exception
     */
    public function create(array $params)
    {
        $data = [
            'cate_name' => $params['cate_name'],
            'sort' => $params['sort'],
            'pid' => 0,
        ];
        return $this->repository->save($data);
    }

    /**
     * 更新信息
     *
     * @param array $data
     * @param $id
     * @return \app\common\model\BaseModel|bool|int
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function update(array $params, $id)
    {
        if(empty($id)) {
            throw new AdminException('ID 不能为空');
        }
        $data = [
            'cate_name' => $params['cate_name'],
            'sort' => $params['sort'],
            'pid' => 0,
        ];
        return $this->repository->update($data, $id);
    }

    /**
     * 删除信息
     *
     * @param $id
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function delete($id)
    {
        return $this->repository->delete($id);
    }
}